home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Sys / Hostname.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  2.0 KB  |  96 lines

  1. package Sys::Hostname;
  2.  
  3. use Carp;
  4. require Exporter;
  5. @ISA = qw(Exporter);
  6. @EXPORT = qw(hostname);
  7.  
  8. =head1 NAME
  9.  
  10. Sys::Hostname - Try every conceivable way to get hostname
  11.  
  12. =head1 SYNOPSIS
  13.  
  14.     use Sys::Hostname;
  15.     $host = hostname;
  16.  
  17. =head1 DESCRIPTION
  18.  
  19. Attempts several methods of getting the system hostname and
  20. then caches the result.  It tries C<syscall(SYS_gethostname)>,
  21. C<`hostname`>, C<`uname -n`>, and the file F</com/host>.
  22. If all that fails it C<croak>s.
  23.  
  24. All nulls, returns, and newlines are removed from the result.
  25.  
  26. =head1 AUTHOR
  27.  
  28. David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
  29.  
  30. Texas Instruments
  31.  
  32. =cut
  33.  
  34. sub hostname {
  35.  
  36.   return $host if defined $host;
  37.  
  38.   if ($^O eq 'VMS') {
  39.  
  40.     eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] };
  41.     if ($@) { return $host = $ENV{'SYS$NODE'}; }
  42.  
  43.     $host = $ENV{'ARPANET_HOST_NAME'}  || $ENV{'INTERNET_HOST_NAME'} ||
  44.             $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'}      ||
  45.             $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
  46.     return $host if $host;
  47.  
  48.     my($rslt) = `hostname`;
  49.     if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
  50.     return $host if $host;
  51.  
  52.     $host = '';
  53.     Carp::croak "Cannot get host name of local machine";  
  54.  
  55.   }
  56.   elsif ($^O eq 'MSWin32') {
  57.     ($host) = gethostbyname('localhost');
  58.     chomp($host = `hostname 2> NUL`) unless defined $host;
  59.     return $host;
  60.   }
  61.   else {  # Unix
  62.  
  63.     eval {
  64.     local $SIG{__DIE__};
  65.     {
  66.         package main;
  67.         require "syscall.ph";
  68.     }
  69.     $host = "\0" x 65; ## preload scalar
  70.     syscall(&main::SYS_gethostname, $host, 65) == 0;
  71.     }
  72.  
  73.     || eval {
  74.     local $SIG{__DIE__};
  75.     $host = `(hostname) 2>/dev/null`; # bsdish
  76.     }
  77.  
  78.     || eval {
  79.     local $SIG{__DIE__};
  80.     $host = `uname -n 2>/dev/null`; ## sysVish
  81.     }
  82.  
  83.     || eval {
  84.     local $SIG{__DIE__};
  85.     ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
  86.     }
  87.  
  88.     || Carp::croak "Cannot get host name of local machine";  
  89.  
  90.     $host =~ tr/\0\r\n//d;
  91.     $host;
  92.   }
  93. }
  94.  
  95. 1;
  96.